package com.mtz.controller;

import com.mtz.VO.ResultVO;
import com.mtz.dateobject.UserWechat;
import com.mtz.exception.MtzException;
import com.mtz.form.WechatForm;
import com.mtz.service.UserWechatService;
import com.mtz.utils.ResultVOUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author 煜赜
 * @date 2018/12/4 6:18
 */
@Api(description = "微信用户接口")
@RestController
@RequestMapping("/user/wechat")
public class UserWechatController {

    @Autowired
    private UserWechatService userWechatService;

    /**
     * 通过openid查找用户
     * @param openid 微信openid
     * @return
     */
    @ApiOperation(value = "通过openid查找用户", notes = "通过openid查找用户")
    @GetMapping("/openid")
    public ResultVO findByOpenid(@ApiParam(value = "微信openid",required = true)
                                     @RequestParam("openid") String openid) {
        UserWechat userWechat = userWechatService.findByOpenid(openid);
        return ResultVOUtil.success(userWechat);
    }

    /**
     * 通过username查找用户
     * @param username 教务在线用户名
     * @return
     */
    @ApiOperation(value = "通过教务在线用户名查找用户", notes = "通过教务在线用户名查找用户")
    @GetMapping("/username")
    public ResultVO findByUsername(@ApiParam(value = "教务在线用户名",required = true)
                                       @RequestParam("username") String username) {
        UserWechat userWechat = userWechatService.findByUsername(username);
        return ResultVOUtil.success(userWechat);
    }

    /**
     * 通过用户类型查找用户
     * @param type 用户类型
     * @return
     */
    @ApiOperation(value = "通过用户类型查找用户", notes = "通过用户类型查找用户")
    @GetMapping("/type")
    public ResultVO findByType(@ApiParam(value = "用户类型",required = true)
                                   @RequestParam("type") Integer type) {
        List<UserWechat> userWechatList = userWechatService.findByType(type);
        return ResultVOUtil.success(userWechatList);
    }

    /**
     * 添加微信用户
     * @param form 微信用户基本信息
     * @param bindingResult
     * @return
     */
    @ApiOperation(value = "添加微信用户", notes = "添加微信用户")
    @PostMapping("/add")
    public ResultVO addUser(@ApiParam(value = "openid,sessionKey,nickname,gender,address",required = true)
                                @Valid WechatForm form, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new MtzException(-1, bindingResult.getFieldError().getDefaultMessage());
        }
        userWechatService.addUser(form);
        return ResultVOUtil.success();
    }

    /**
     * 设置用户地理位置信息
     * @param openid 微信openid
     * @param longitude 经度
     * @param latitude 纬度
     * @return
     */
    @ApiOperation(value = "设置用户地理位置信息", notes = "设置用户地理位置信息")
    @PostMapping("/location")
    public ResultVO setLocation(@ApiParam(value = "微信openid",required = true)
                                    @RequestParam("openid") String openid,
                                @ApiParam(value = "经度",required = true)
                                    @RequestParam("longitude") String longitude,
                                @ApiParam(value = "纬度",required = true)
                                    @RequestParam("latitude") String latitude) {
        userWechatService.setLocation(openid, longitude, latitude);
        return ResultVOUtil.success();
    }
}
